uploadable = true;
fileType="uploaded";
var fp = $("#folderPath");
var mygrid;
$(function() {
	
	$("form.search .datepicker").datepicker({dateFormat:"yy-mm-dd"});
	$("form.search .datepicker").attr("readOnly",true);
	$("form.search #sbtn").click(function(){
		search();
	});
	$("#startFile").bind("click",function(){
		var rval = window.showModalDialog("fileserver/file/uploader?folderId="+folderId);
	   	reloadMe();
	});
	
	
	
	mygrid = $("#list").jqGrid({
		url : "fileserver/file/file-data!typedfile",
		postData : {
			"folder.type":"uploaded",
			"folder.id" : folderId
		},
		datatype : "json",
		mtype : 'POST',
		colNames : [ '选择','查看', '文件名', '类型', '上传日期', '删除' ],
		colModel : [ {
			name : 'id',
			index : 'id',
			width : 10,
			formatter : selectFormatter

		}, {
			name:"id",
			index:'id',
			width:20,
			formatter:editFormatter
			
		},{
			name : 'title',
			index : 'title',
			width : 200,
			formatter : downloadFormatter

		}, {
			name : 'imgpath',
			index : 'imgpath',
			width : 50,
			formatter : imgFormatter
		}, {
			name : 'createTime',
			index : 'createTime',
			width : 100
		// formatter:date
		}, {
			name : 'did',
			index : 'id',
			width : 50,
			formatter : deleteFormater
		}

		],
		pager : '#pager',
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		sortname : 'createTime',
		sortorder : 'desc',
		viewrecords : true,
		caption : '我的文件',

		width : '750',
		height : '300',
		jsonReader : {
			repeatitems : false,
			id : "0"
		}
	});
	mygrid.jqGrid("navGrid", "#pager", {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	

});// end of document ready
function reloadMe(){
	mygrid.trigger("reloadGrid");
}
function selectFormatter(cv, opt, row) {
	var fid = row['id'];
	var title = row['title'];
	var link = $("<input type=\"checkbox\" onclick=\"checkFile('" + fid + "','" + title + "',this.checked);\"></input>");
	return $("<span></span>").append(link).html();

}
function downloadFormatter(cellvalue, options, rowObject) {
	var eid = rowObject['id'];
	var link = $("<a></a>");	
	link.attr("href", "fileserver/file/download!download?bean.id="+ eid);
	link.attr("target", "_blank");
	link.html("<b>" + cellvalue + "</b>");
	return $("<span></span>").append(link).html();

}
function imgFormatter(cellvalue, options, rowObject) {
	var img = $("<img></img>");
	img.attr("src", sbase+cellvalue);
	img.css("width", "16px");
	img.css("height", "16px");
	return $("<span><span>").append(img).html();
}

function deleteFormater(cv, opt, row) {
	var eid = row['id'];
	var link = $("<a></a>");
	link.attr("href", "javascript:removeMe('" + eid + "');");
	link.html("<img src='"+sbase+"images/removeme.gif' style='border:none;'/>");
	return $("<span></span>").append(link).html();
}

function editFormatter(v,opt,row){
	var ftype  =row['fileType'];
	var eid = row['id'];
	var link = $("<a></a>");
	if(ftype=="doc" 
		|| ftype=="docx"
	    || ftype=="xls"
	    || ftype=="xlsx"
	    || ftype=="ppt"
	    || ftype=="pptx"
	){
		link.attr("href", "fileserver/file/translate-file!openforedit?fileId="+eid);
	}else{
		return '-';
	}
	link.attr("target","_blank");
	link.html("<img src='"+sbase+"/images/file-edit.gif' style='border:none' />");
	return $("<span></span>").append(link).html();
}


function loadFolder(fid, ftxt) {
	folderId = fid;
	folderText = ftxt;

	doAjaxLink("fileserver/file/folder!myfolder?bean.id=" + fid, function() {
		loadCallback(fid, ftxt);
	});
}

function loadCallback(fid, ftxt) {

	var cidx = -1;
	$.each(folders, function(idx, folder) {
		if (fid == folder.id) {
			cidx = idx;

			return false;
		}
	});

	if (cidx != -1) {
		folders = folders.slice(0, cidx + 1);
	} else {
		folders.push({
			id : fid,
			dir : ftxt
		});
	}
	$.each(folders, function(idx, folder) {
		var pa = $("<a></a>");
		pa.html(folder['dir']);
		pa.attr("href", "javascript:void(0);");
		try {
			(function(k, t) {

				pa.get(0).onclick = function() {
					loadFolder(k, t);
				}

			})(folder['id'], folder['dir']);
		} catch (e) {
			alert(e.message);
		}
		fp.append(pa).append(">");

	});

}



function removeFolder(id, text) {
	var rst = confirm("确定要删除该文件夹,如果删除,该文件加下的所有文件和文件夹都会被删除\n 按 '确定' 删除该文件夹,'取消' 不删除!!");
	if (!rst)
		return;
	
	$.ajax({
		url : "fileserver/file/folder!remove",
		data : "bean.id=" + id,
		success : function() {
			createNotify("文件夹  " + text + " 已删除!");			
			loadFolder(folderId, folderText);
		},
		error : function(d, ts, jq) {		
			alert("删除失败:" + d);
		}
	})
}

function search(){
	var ps = $("form.search").serialize();
	//alert(ps);
	var objs = ps.split("&");
	var pms = {};
	for(var i=0;i<objs.length;i++){
		var oa = objs[i].split("=");
		pms[decodeURIComponent(oa[0])]=decodeURIComponent(oa[1]);
	}
	mygrid.jqGrid('setGridParam',{url:dataurl,postData:pms}).trigger("reloadGrid");
}